/**
 * 
 */
package com.projectsquirrel;

import java.io.OutputStreamWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Arthi
 *
 */
public class photoUpload extends Activity{
	
	private static final int SELECT_PICTURE = 1;
	private static final int VIEW_PICTURE = 2; 
	public static final int VISIBLE = 1;
	
	final String TAG = "Project_2_Group_6";

	static int flag = 0;
	String selectedImagePath;
    String filemanagerstring;
    String finalFilePath;
    Bitmap bm; 
    
    static Button submitButton;
    static ImageView imageViewer;
    
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.photoupload);

	        //Get data captured from newObservation.java
	        //newObs = newObservation.getResults();
	        
	        // capture our View elements
	        
	        final ImageButton settingsIconButton = (ImageButton) findViewById(R.id.settingnfoScreenIcon);
	        final ImageButton infoIconButton = (ImageButton) findViewById(R.id.infoScreenIcon);
	        final Button browsePhoto = (Button) findViewById(R.id.photoBrowseButton);
	        submitButton = (Button) findViewById(R.id.photoSubmitButton);
	        imageViewer = (ImageView) findViewById(R.id.imageContainer);
	        
	        
	        
	        settingsIconButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent i = new Intent(photoUpload.this, settingsMenu.class);
			        startActivity(i);
				}	    	
		    });
	        
		    infoIconButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent i = new Intent(photoUpload.this, infoMenu.class);
			        startActivity(i);
				}	    	
		    });
		    
		    browsePhoto.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// select a file
	                Intent intent = new Intent();
	                intent.setType("image/*");
	                intent.setAction(Intent.ACTION_GET_CONTENT);
	                startActivityForResult(Intent.createChooser(intent,
	                        "Select Picture"), SELECT_PICTURE);
	                
	                
				}
				
		    });
		    
		    
		    
		    submitButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if(flag==0){
						Toast.makeText(getApplicationContext(),"!Please select a picture!",Toast.LENGTH_LONG).show();
					}
					else
					{
						try{
							Log.i(TAG, finalFilePath);
			
						}
						catch(Throwable t) {
							Toast.makeText(getApplicationContext(), "Exception: "+ t.toString(), 5000).show();
						}
						Toast.makeText(getApplicationContext(),"Photo Submitted. Thank you!",Toast.LENGTH_LONG).show();
						finish();
					}
					
				}	    	
		    });
		    
	 }
		    @Override
		    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		        super.onActivityResult(requestCode, resultCode, data);
		        
		        if (resultCode == RESULT_OK) {
		            if (requestCode == SELECT_PICTURE) {
		                Uri selectedImageUri = data.getData();

		                //OI FILE Manager
		                filemanagerstring = selectedImageUri.getPath();
		            
		                //MEDIA GALLERY
		                selectedImagePath = getPath(selectedImageUri);                
		                
		                if(selectedImagePath!=null){
		                	finalFilePath = selectedImagePath;
		                }
		                else{
		                	finalFilePath = filemanagerstring;
		                }
		                   
		                bm = BitmapFactory.decodeFile(finalFilePath);  
		                imageViewer.setImageBitmap(bm); 
		                submitButton.setVisibility(VISIBLE);
		                
		                flag = 1;         
		                          
		            }
		        }
		    }

	public String getPath(Uri uri) {
		        String[] projection = { MediaStore.Images.Media.DATA };
		        Cursor cursor = managedQuery(uri, projection, null, null, null);
		        if(cursor!=null)
		        {
		            //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
		            //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
		            int column_index = cursor
		            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		            cursor.moveToFirst();
		            return cursor.getString(column_index);
		        }
		        else return null;
		    }
	public void OnStart(){
		super.onStart();
		
		Calendar calendar = new GregorianCalendar();
		/*
 	    int hour = calendar.get(Calendar.HOUR); 		
 	    if(hour>18 || hour<10)
 		{
 			TableLayout l = (TableLayout) findViewById(R.id.Screen);
 			int color=Color.WHITE;
 			l.setBackgroundColor(color);
 			
 		}
		*/
	}
	 
}
